mw.Upload.BookletLayout: Move error checking for uploadToStash to uploadFile
authorPrateek Saxena <prtksxna@gmail.com>
Wed, 30 Sep 2015 19:38:38 +0000 (01:08 +0530)
committerPrtksxna <psaxena@wikimedia.org>
Wed, 30 Sep 2015 19:55:44 +0000 (19:55 +0000)
We were checking for uploadToStash errors in saveFile, instead of in
uploadFile itself. Apart from being just wrong it was causing two
issues:

1. We would show an upload error until after you've tried to save the
   file.

2. If there was an error in saving, and you try again, the first thing
   it would do is check mw.Upload.State to hopefully check for
   uploadToStash errors. Instead of running the finishStashUpload again
   it would fail because of the earlier error, thus making all errors
   unrecoverable.

Bug: T114130
Change-Id: I56f4ef6e6536840a63402688eb2a1875a6876fcc

resources/src/mediawiki/mediawiki.Upload.BookletLayout.js

index 41200d3..fa2fb0b 100644 (file)
         * @return {jQuery.Promise}
         */
        mw.Upload.BookletLayout.prototype.uploadFile = function () {
-               var file = this.getFile();
+               var deferred = $.Deferred(),
+                       layout = this,
+                       file = this.getFile();
 
                this.filenameWidget.setValue( file.name );
                this.setPage( 'info' );
 
                this.upload.setFile( file );
                this.uploadPromise = this.upload.uploadToStash();
-               this.uploadPromise.then( this.emit.bind( this, 'fileUploaded' ) );
+               this.uploadPromise.then( function () {
+                       deferred.resolve();
+                       layout.emit( 'fileUploaded' );
+               } );
+               this.uploadPromise.always( function () {
+                       if ( layout.upload.getState() === mw.Upload.State.ERROR ) {
+                               deferred.reject( new OO.ui.Error( mw.msg( 'upload-process-error' )  ) );
+                               return false;
+                       }
+                       if ( layout.upload.getState() === mw.Upload.State.WARNING ) {
+                               deferred.reject( new OO.ui.Error( mw.msg( 'upload-process-error' )  ) );
+                               return false;
+                       }
+               } );
 
-               return this.uploadPromise;
+               return deferred;
        };
 
        /**
                this.upload.setText( this.getText() );
 
                this.uploadPromise.always( function () {
-
-                       if ( layout.upload.getState() === mw.Upload.State.ERROR ) {
-                               deferred.reject( new OO.ui.Error( mw.msg( 'upload-process-error' )  ) );
-                               return false;
-                       }
-
-                       if ( layout.upload.getState() === mw.Upload.State.WARNING ) {
-                               deferred.reject( new OO.ui.Error( mw.msg( 'upload-process-error' )  ) );
-                               return false;
-                       }
-
                        layout.upload.finishStashUpload().always( function () {
                                var name;